package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.cache.RedisCache;
import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodService vodService;

	@Operation(summary = "音视频上传点播平台")
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String,String>> uploadTrack(@RequestParam("file") MultipartFile file){
		Map<String,String> result = vodService.uploadTrack(file);

		return Result.ok(result);
	}

	/**
	 * @param trackInfoVo
	 * @return
	 */
	@UserLogin
	@Operation(summary = "保存声音")
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@Validated @RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	@UserLogin
	@Operation(summary = "条件分页查询当前用户声音列表（包含声音统计信息）")
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable("page") int page,
													   @PathVariable("limit") int limit,
													   @RequestBody TrackInfoQuery query){
		//获取当前用户名id
		Long userId = AuthContextHolder.getUserId();
		query.setUserId(userId);

		Page<TrackListVo> pageInfo = new Page<>(page,limit);
		Page<TrackListVo> result = trackInfoService.findUserTrackPage(pageInfo, query);

		return Result.ok(result);
	}

	@Operation(summary = "修改声音信息")
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id,
								  @RequestBody TrackInfo trackInfo){
		trackInfoService.updateTrackInfo(trackInfo);
		return Result.ok();
	}

	@Operation(summary = "根据声音ID查询声音信息")
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	@RedisCache(prefix = "album:trackInfo:")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	@Operation(summary = "删除声音信息")
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	@UserLogin(required = false)
	@Operation(summary = "分页查询专辑下声音列表（动态渲染付费标识）")
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
															  @PathVariable Long page,
															  @PathVariable Long limit){
		IPage<AlbumTrackListVo> pageInfo = new Page<>(page,limit);
		IPage<AlbumTrackListVo> result = trackInfoService.findAlbumTrackPage(pageInfo,albumId);
		return Result.ok(result);
	}


	/**
	 * 查询声音统计信息
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "获取声音统计信息")
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable("trackId") Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}


	/**
	 *  查询用户未购买声音列表
	 * @param trackId 选择购买声音ID，作为起始标准计算未购买声音数量
	 * @param trackCount 限制查询未购买声音数量
	 * @return
	 */
	@UserLogin
	@Operation(summary = "查询用户未购买声音列表")
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId,
														 @PathVariable("trackCount") Integer trackCount){
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(userId,trackId,trackCount);
		return Result.ok(list);
	}

}

